#include "opencv2/opencv.hpp"
#include "myclass.h"
#include <vector>  
using namespace cv;
using namespace std;

void FirstClass() //第一课 显示 缩放  颜色空间
{
	const char *fn = "lena.jpg";//文件路径
	Mat image = imread(fn);//读取文件
	Mat imagaGauss, image1, image2, gray, gray1, hsv, hsvChannels[3];
	Mat rgb, Channels[3],rgbChannels[3],display_R, display_G,display_B;
	Mat zeros = Mat::zeros(image.rows, image.cols, CV_8UC1);
	//对图像做高斯平滑处理并显示
	GaussianBlur(image, imagaGauss, Size(5, 5), 0);
	imshow("Hello World!", image);
	//imshow("Gaussian filtered image", imagaGauss);
	waitKey();//等待输入
	//对图像使用resize和pyDown缩小一半，再缩小一半，显示各图像
	resize(image, image1, Size(image.cols / 2, image.rows / 2));
	//pyrDown(image1, image2);
	//imshow("image1", image1);
	//imshow("image2", image2);
	//waitKey();//等待输入
	//cvtColor 将源图按照某种形式转化成目标图
	//彩色转灰度并作阈值化，显示
	cvtColor(image1, gray, COLOR_BGR2GRAY);
	//二值化
	threshold(gray, gray1, 120, 0xff, THRESH_BINARY);
	imshow("gray", gray);

	
	cvtColor(image1,hsv,COLOR_BGR2HSV);
	split(hsv,hsvChannels);//通道分离
	imshow("Hue", hsvChannels[0]);//色调
	imshow("Saturation",hsvChannels[1]);//饱和度
	imshow("Value",hsvChannels[2]);//亮度
	waitKey();
	destroyAllWindows();//关闭所有窗口
}
void splitRGB(const char *fn)
{
	    cv::Mat image = imread(fn);//读取文件
		cv::Mat src;
		resize(image, src, Size(image.cols / 2, image.rows / 2));
		cv::imshow("src", src);

		// Split the image into different channels
		std::vector<cv::Mat> rgbChannels(3);
		split(src, rgbChannels);

		// Show individual channels
		cv::Mat blank_ch, fin_img;
		blank_ch = cv::Mat::zeros(cv::Size(src.cols, src.rows), CV_8UC1);
		
		// Showing Red Channel
		// G and B channels are kept as zero matrix for visual perception
		std::vector<cv::Mat> channels_r;
		channels_r.push_back(blank_ch);
		channels_r.push_back(blank_ch);
		channels_r.push_back(rgbChannels[2]);
		/// Merge the three channels
		cv::merge(channels_r, fin_img);
		cv::imshow("R", fin_img);
		// Showing Green Channel
		std::vector<cv::Mat> channels_g;
		channels_g.push_back(blank_ch);
		channels_g.push_back(rgbChannels[1]);
		channels_g.push_back(blank_ch);
		cv::merge(channels_g, fin_img);
		cv::imshow("G", fin_img);
		// Showing Blue Channel
		std::vector<cv::Mat> channels_b;
		channels_b.push_back(rgbChannels[0]);
		channels_b.push_back(blank_ch);
		channels_b.push_back(blank_ch);
		cv::merge(channels_b, fin_img);
		cv::imshow("B", fin_img);
		cv::waitKey(0);	
}

int main(void)
{
	FirstClass();//第一课
	splitRGB("lena.jpg");
}

